package a06_栈与队列;

import java.util.Stack;

/**
 * <p>
 * a06_逆波兰表达式求值复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/2/9
 */
public class a06_逆波兰表达式求值复习3 {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String token : tokens) {
            if(token.equals("+") || token.equals("-") || token.equals("*") || token.equals("/")){
                Integer num2 = stack.pop();
                Integer num1 = stack.pop();
                if(token.equals("+")){
                    stack.push(num1 + num2);
                }else if(token.equals("-")){
                    stack.push(num1 - num2);
                }else if(token.equals("*")){
                    stack.push(num1 * num2);
                }else if(token.equals("/")){
                    stack.push(num1 / num2);
                }
            }else {
                stack.push(Integer.valueOf(token));
            }
        }
        return stack.pop();
    }
}
